package com.tanhua.server.service;

import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.Constants;
import com.tanhua.dubbo.api.db.MessageApi;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.dubbo.api.db.UserApi;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.model.db.Announcement;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class MessagesService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private SettingApi settingApi;

    @DubboReference
    private MessageApi messageApi;

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private UserLikeApi userLikeApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 获取评论
     *
     * @param page
     * @param size
     * @return
     */
    public PageResult getComments(Long page, Long size) {
        //获取评论列表
        PageResult<Comment> comment = commentApi.getCommentAll(page, size, UserHolder.getUserId());
        //获取评论用户ID，查询用户详情
        List<Comment> items = comment.getItems();
        List<MessagesCommentVo> voList = new ArrayList<>();
        for (Comment comments : items) {
            UserInfo userInfo = userInfoApi.getuserinfo(comments.getPublishUserId(), null);
            MessagesCommentVo vo = new MessagesCommentVo();
            vo.setId(String.valueOf(comments.getId()));
            vo.setAvatar(userInfo.getAvatar());
            Date date = new Date(comments.getCreated());
            vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            vo.setNickname(userInfo.getNickname());
            voList.add(vo);
        }
        return new PageResult(comment.getPage(), comment.getPagesize(), comment.getCounts(), voList);
    }

    /**
     * 获取点赞数
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getLikes(Long page, Long pagesize) {
        PageResult<UserLike> loves = userLikeApi.getLoves(page, pagesize, UserHolder.getUserId());
        List<UserLike> items = loves.getItems();
        List<UserLikeVo> voList = items.stream().map(itm->{
            UserInfo userInfo = userInfoApi.getuserinfo(itm.getUserId(), null);
            UserLikeVo init = UserLikeVo.init(userInfo, itm);
            return init;
        }).collect(Collectors.toList());
        return new PageResult(loves.getPage(), loves.getPagesize(), loves.getCounts(), voList);
    }

    /**
     * 获取公告
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getAnnouncements(Long page, Long pagesize) {
        Settings settings = settingApi.getNotification(UserHolder.getUserId());
        if (settings.getGonggaoNotification()) {
            PageResult<Announcement> announcement = messageApi.getAnnouncement(page, pagesize);
            List<Announcement> items = announcement.getItems();
            List<AnnouncementVo> voList = new ArrayList<>();
            for (Announcement item : items) {
                AnnouncementVo init = AnnouncementVo.init(item.getId(), item.getTitle(), item.getDescription(), item.getCreated());
                voList.add(init);
            }

            return new PageResult(announcement.getPage(), announcement.getPagesize(), announcement.getCounts(), voList);
        }
        return null;
    }

    /**
     * 根据环信id查询用户信息
     *
     * @param huanxinId
     * @return
     */
    public UserInfoVo findByHxId(String huanxinId) {

        User user = userApi.findByHxId(Constants.HX_USER_PREFIX + huanxinId);
        UserInfo getuserinfo = userInfoApi.getuserinfo(user.getId(), null);
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(getuserinfo, vo);
        return vo;
    }

    /**
     * 添加联系人
     *
     * @param params
     */
    public void addContacts(Map<String, Integer> params) {
        Integer userId = params.get("userId");
        friendApi.addContacts(userId.longValue(), UserHolder.getUserId());
        huanXinTemplate.addContact(UserHolder.getUser().getHxUser(), Constants.HX_USER_PREFIX + userId);
    }

    /**
     * 获取好友列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getContacts(Long page, Long pagesize) {
        List<Friend> result = friendApi.getContacts(page, pagesize, UserHolder.getUserId());
        List<Long> longList = result.stream().map(Friend::getFriendId).collect(Collectors.toList());
        List<UserInfo> usersByid = userInfoApi.getUsersByid(longList);
        List<UserInfoVoContacts> voList = new ArrayList<>();
        for (UserInfo userInfo : usersByid) {
            UserInfoVoContacts init = UserInfoVoContacts.init(userInfo);
            voList.add(init);
        }
        Integer size = voList.size();
        return new PageResult(page, pagesize, size.longValue(), voList);
    }

    /**
     *      获取被喜欢列表
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getLoves(Long page, Long pagesize) {
        PageResult<UserLike> userLike = userLikeApi.getLoves(page,pagesize,UserHolder.getUserId());
        List<UserLike> items = userLike.getItems();
        List<UserLikeVo> voList = new ArrayList<>();
        for (UserLike like : items) {
            UserInfo userInfo = userInfoApi.getuserinfo(like.getUserId(), null);
            UserLikeVo init = UserLikeVo.init(userInfo,like);
            voList.add(init);
        }
        return new PageResult(page,pagesize, userLike.getCounts(), voList);
    }
}
